package com.fitmanagement.controller;


import com.fitmanagement.entity.Result;
import com.fitmanagement.entity.Usercard;
import com.fitmanagement.service.IUsercardService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 虚空
 * @since 2024-11-11
 */
@RestController
@RequestMapping("/usercard")
@CrossOrigin
@RequiredArgsConstructor
public class UsercardController {
private final IUsercardService usercardService;
    @GetMapping("/getAll")
    public List<Map<String ,Object>> getAll(){
        List<Map<String, Object>> maps = usercardService.listMaps();
        System.out.println(maps);
        return maps;
    }


    @GetMapping("/getname/{id}")
    public String getname(@PathVariable("id") int id){

        return usercardService.selectColumn(id);
    }
@PutMapping("/update/{id}")
public Result<Usercard> updateStatus(@PathVariable("id") Integer id, @RequestBody @Validated Usercard usercard) {
    // 打印接收到的请求数据
    System.out.println("Updating Usercard with ID: " + id);
    System.out.println("Usercard Data: " + usercard);

    // 调用服务层方法更新 Usercard 信息
    boolean updateSuccess = usercardService.updateCard(id, usercard.getBlance(), usercard.getMaturityTime(), usercard.getParchTime());

    // 检查更新是否成功
    if (updateSuccess) {
        return Result.success();
    } else {
       return Result.error("更新失败", "未知错误");
    }
}




}
